//
//  GMFBottomToolbarView.m
//  SplunkMobile
//
//  Created by lijunying on 1/28/2016.
//  Copyright © 2016 Gome. All rights reserved.
//

#import "GMFBottomToolbarView.h"
#import "GMFColorButton.h"

@interface GMFBottomToolbarView()

/// 记录有几个按钮
@property (nonatomic, assign) int cintButtonCount;
@property (nonatomic, weak) UIView *viewSeperateLine;
@property (nonatomic, weak) UIView *viewRed;

@end

@implementation GMFBottomToolbarView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = GMKColor(51, 51, 51);
        self.alpha = 0.95;
        
        _intPosition = 2;
        
        /// 调色板
        GMFColorButton *btnPalette = [[GMFColorButton alloc] init];
        [btnPalette setRadius:intNormalCircleRadius andColor:kColorDefaultBlue andHeight:YES];
        btnPalette.imageEdgeInsets = UIEdgeInsetsMake(-5,10,10,10);
        
        [btnPalette setTitle:@"颜色" forState:UIControlStateNormal];
        btnPalette.titleLabel.font = [UIFont systemFontOfSize:15];
        btnPalette.titleEdgeInsets = UIEdgeInsetsMake(25, 0, 0, 0);
        btnPalette.tag = 2000;
        [btnPalette addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnPalette];
        self.btnPalette = btnPalette;
        _cintButtonCount++;
        
        /// 箭头
        UIButton *btnArrow = [[UIButton alloc] init];
        [btnArrow setImage:[UIImage imageNamed:@"btn_arrow"] forState:UIControlStateNormal];

        btnArrow.imageEdgeInsets = UIEdgeInsetsMake(5,15,18,15);
        
        [btnArrow setTitle:@"箭头" forState:UIControlStateNormal];
        btnArrow.titleLabel.font = [UIFont systemFontOfSize:15];
        btnArrow.titleEdgeInsets = UIEdgeInsetsMake(25, -27, 0, 0);
        btnArrow.tag = 2001;
        [btnArrow addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnArrow];
        self.btnArrow = btnArrow;
        _cintButtonCount++;
        
        
        /// 画图
        UIButton *btnDraw = [[UIButton alloc] init];
        [btnDraw setImage:[UIImage imageNamed:@"btn_curve2"] forState:UIControlStateNormal];
        
        btnDraw.imageEdgeInsets = UIEdgeInsetsMake(5,15,18,15);
        
        [btnDraw setTitle:@"自由线" forState:UIControlStateNormal];
        btnDraw.titleLabel.font = [UIFont systemFontOfSize:15];
        btnDraw.titleEdgeInsets = UIEdgeInsetsMake(25, -27, 0, 0);
        [btnDraw setTitleColor:kColorDefaultRed forState:UIControlStateNormal];
        btnDraw.tag = 2002;
        [btnDraw addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnDraw];
        self.btnDraw = btnDraw;
        _cintButtonCount++;
        
        /// 编辑文本
        UIButton *btnAddText = [[UIButton alloc] init];
        [btnAddText setImage:[UIImage imageNamed:@"btn_text"] forState:UIControlStateNormal];
        
        btnAddText.imageEdgeInsets = UIEdgeInsetsMake(5,15,18,15);
        
        [btnAddText setTitle:@"批注" forState:UIControlStateNormal];
        btnAddText.titleLabel.font = [UIFont systemFontOfSize:15];
        btnAddText.titleEdgeInsets = UIEdgeInsetsMake(25, -27, 0, 0);
        btnAddText.tag = 2003;
        [btnAddText addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnAddText];
        self.btnAddText = btnAddText;
        _cintButtonCount++;
        
//        UIView *viewSeperateLine = [[UIView alloc] init];
//        viewSeperateLine.backgroundColor = GMKColor(210, 210, 210);
//        [self addSubview:viewSeperateLine];
//        self.viewSeperateLine = viewSeperateLine;
        
//        UIView *viewRed = [[UIView alloc] init];
//        viewRed.backgroundColor = kColorDefaultRed;
//        [self addSubview:viewRed];
//        self.viewRed = viewRed;
        
    }
    
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat fltPadding = 15;
    CGFloat fltPaddingBetweenButtons = (screenW - fltPadding * 2 - fltButtonWidth * _cintButtonCount) / (_cintButtonCount - 1);
    
    [_btnPalette        setFrame:CGRectMake(fltButtonWidth * 0 + fltPadding + fltPaddingBetweenButtons * 0, 0, fltButtonWidth, fltButtonWidth - 4)];
    [_btnArrow          setFrame:CGRectMake(fltButtonWidth * 1 + fltPadding + fltPaddingBetweenButtons * 1, 0, fltButtonWidth, fltButtonWidth - 4)];
    [_btnDraw           setFrame:CGRectMake(fltButtonWidth * 2 + fltPadding + fltPaddingBetweenButtons * 2, 0, fltButtonWidth, fltButtonWidth - 4)];
    [_btnAddText        setFrame:CGRectMake(fltButtonWidth * 3 + fltPadding + fltPaddingBetweenButtons * 3, 0, fltButtonWidth, fltButtonWidth - 4)];
    
    [_viewRed           setFrame:CGRectMake(fltButtonWidth * 2 + fltPadding + fltPaddingBetweenButtons * 2, 44 - 4, fltButtonWidth, 4)];
    
//    [_viewSeperateLine  setFrame:CGRectMake(0, 0, screenW, 1)];
}


- (void)buttonClick:(UIButton *)button
{
    NSString *string = [NSString stringWithFormat:@"%ld", (long)button.tag];
    
    if ([self.delegate respondsToSelector:@selector(bottomToolbarView:buttonTagString:)])
    {
        [self.delegate bottomToolbarView:self buttonTagString:string];
    }
}


- (void)changeRedViewPosition:(NSInteger)position
{
    NSInteger intMinus = labs(_intPosition - position);
    
    NSTimeInterval duration = intMinus * 0.3;
    
    if (intMinus == 0)
    {
        return;
    }
    
    CGFloat fltPadding = 15;
    CGFloat fltPaddingBetweenButtons = (screenW - fltPadding * 2 - fltButtonWidth * _cintButtonCount) / (_cintButtonCount - 1);
    
    [UIView animateWithDuration:duration animations:^{
        [_viewRed setFrame:CGRectMake(fltButtonWidth * position + 12 + fltPaddingBetweenButtons * position, 44 - 4, fltButtonWidth, 4)];
    } completion:^(BOOL finished) {
        
    }];
    
    _intPosition = position;
}


@end
















